package com.pan.admin.module.channel.service.impl;

import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.annotation.DataSource;
import com.pan.admin.common.enums.DataSourceType;
import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.common.utils.DictUtils;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.common.utils.uuid.IdUtils;
import com.pan.admin.module.channel.dao.LoanChannelDao;
import com.pan.admin.module.channel.dao.LoanChannelFlowConfigDao;
import com.pan.admin.module.channel.dao.LoanChannelMarketDao;
import com.pan.admin.module.channel.entity.LoanChannel;
import com.pan.admin.module.channel.entity.LoanChannelFlowConfig;
import com.pan.admin.module.channel.entity.LoanChannelMarket;
import com.pan.admin.module.channel.service.LoanChannelService;
import com.pan.admin.module.market.dao.LoanMarketDao;
import com.pan.admin.module.market.entity.LoanMarket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 渠道Service业务层处理
 *
 * @author ts
 * @date 2023-09-11
 */
@Service
public class LoanChannelServiceImpl implements LoanChannelService {

    @Autowired
    private LoanChannelDao loanChannelDao;
    @Autowired
    private LoanChannelFlowConfigDao loanChannelFlowConfigDao;
    @Autowired
    private LoanChannelMarketDao channelMarketDao;
    @Autowired
    private LoanMarketDao loanMarketDao;

    @Override
    @Transactional(readOnly = true)
    public List<Map<String, Object>> listLoanChannel(String keyword) {
        return this.loanChannelDao.listLoanChannel(keyword);
    }

    /**
     * 根据ID获取列表渠道
     *
     * @param id 渠道主键
     * @return 渠道
     */
    @Override
    public LoanChannel getById(Long id) {
        return loanChannelDao.getById(id);
    }

    /**
     * 查询渠道列表
     *
     * @param loanChannel 渠道
     * @return 渠道
     */
    @Override
    @Transactional(readOnly = true)
    public List<LoanChannel> list(LoanChannel loanChannel) {
        return loanChannelDao.list(loanChannel);
    }

    @Override
    @Transactional(readOnly = true)
    @DataSource(DataSourceType.SLAVE)
    public List<LoanChannel> listSlave(LoanChannel loanChannel) {
        return loanChannelDao.list(loanChannel);
    }

    /**
     * 新增渠道
     *
     * @param loanChannel 渠道
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertLoanChannel(LoanChannel loanChannel) {

        //set 渠道字段参数
        String versionLabel = DictUtils.getDictLabel("channel_version", loanChannel.getVersion().toString());
        String versionDomain = DictUtils.getDictValue("common_domain", versionLabel);
        String uniqueCode = IdUtils.fastSimpleUUID();
        loanChannel.setUniqueCode(uniqueCode);
        loanChannel.setCreateTime(DateUtils.getNowDate());
        loanChannel.setLink(versionDomain + uniqueCode);
        loanChannel.setCreateBy(SecurityUtils.getUsername());
        // 新增渠道
        int row = loanChannelDao.insertLoanChannel(loanChannel);

        // 新增渠道关联贷超
        insertChannelMarket(loanChannel);

        // 新增渠道流程控制
        insertChannelFlowConfig(loanChannel);

        if (row < 1) {
            throw new RuntimeException("回滚");
        }
        return row;
    }


    /**
     * 修改渠道
     *
     * @param loanChannel 渠道
     * @return 结果
     */
    @Override
    public int updateLoanChannel(LoanChannel loanChannel) {
        loanChannel.setUpdateTime(DateUtils.getNowDate());
        loanChannel.setUpdateBy(SecurityUtils.getUsername());
        return loanChannelDao.updateLoanChannel(loanChannel);
    }

    /**
     * 新增渠道关联贷超
     *
     * @param loanChannel
     * @param row
     * @return
     */
    private void insertChannelMarket(LoanChannel loanChannel) {
        Integer channelMarketState = loanChannel.getChannelMarketState();
        if (ValidParam.isNotNull(channelMarketState)) {
            //  channelMarketState为0 贷超全选
            if (channelMarketState == 0) {
                List<LoanMarket> marketList = loanMarketDao.list(null);
                List<LoanChannelMarket> list = new ArrayList<>();
                if (!ValidParam.isEmpty(marketList)) {
                    for (LoanMarket loanMarket : marketList) {
                        LoanChannelMarket channelMarket = new LoanChannelMarket();
                        channelMarket.setLoanMarketId(loanMarket.getId());
                        channelMarket.setLoanChannelId(loanChannel.getId());
                        channelMarket.setCreateTime(DateUtils.getNowDate());
                        channelMarket.setStatus(1);
                        channelMarket.setCreateBy(SecurityUtils.getUsername());
                        list.add(channelMarket);
                    }
                    channelMarketDao.batchInsert(list);
                }
            }
        }
    }

    /**
     * 新增渠道流程控制
     *
     * @param loanChannel
     * @param row
     * @return
     */
    private void insertChannelFlowConfig(LoanChannel loanChannel) {
        LoanChannelFlowConfig flowConfig = new LoanChannelFlowConfig();
        flowConfig.setLoanChannelId(loanChannel.getId());
        flowConfig.setStatus(1L);
        flowConfig.setCreateBy(SecurityUtils.getUsername());
        flowConfig.setCreateTime(DateUtils.getNowDate());
        loanChannelFlowConfigDao.insertLoanChannelFlowConfig(flowConfig);
    }
}
